home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / gnustuff / minix / libsrc~1.z / libsrc~1 / strncmp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-12-28  |  792 b   |  42 lines

  1. #include "lib.h"
  2.  
  3. /*
  4.  * strncmp - compare at most n characters of string s1 to s2
  5.  */
  6.  
  7. int                /* <0 for <, 0 for ==, >0 for > */
  8. strncmp(s1, s2, n)
  9. _CONST char *s1;
  10. _CONST char *s2;
  11. _SIZET n;
  12.   {
  13.     register _CONST char *scan1;
  14.     register _CONST char *scan2;
  15.     register _SIZET count;
  16.  
  17.     scan1 = s1;
  18.     scan2 = s2;
  19.     count = n;
  20.     while (count > 0 && *scan1 != '\0' && *scan1 == *scan2) {
  21.         scan1++;
  22.         scan2++;
  23.         count--;
  24.     }
  25.     if (count == 0)
  26.         return(0);
  27.  
  28.     /*
  29.      * The following case analysis is necessary so that characters
  30.      * which look negative collate low against normal characters but
  31.      * high against the end-of-string NUL.
  32.      */
  33.     if (*scan1 == '\0' && *scan2 == '\0')
  34.         return(0);
  35.     else if (*scan1 == '\0')
  36.         return(-1);
  37.     else if (*scan2 == '\0')
  38.         return(1);
  39.     else
  40.         return(*scan1 - *scan2);
  41.   }
  42.